#pragma once
#include "core/core.h"

// 连接时发送设备校验
#define UART_CHECK_CODE "MAICONTROL"

// 指令宽度
#define COMMAND_LENGE 10

class Serial_uart
{
public:
    static void init(bool idle = false);
    static void change_speed(int32_t speed);
    static void set_sensitivity(int32_t point, int32_t value);
    static void set_pressure(int32_t point, int32_t value);
    static void idle();
    static void send(const String call, const uint8_t enter = 1);
    static void send(const int32_t call, const uint8_t enter = 1);
    static String get_string(String chr);
    static int32_t get_int(String array);
    static int32_t uart_speed;

    // 串口流控用 稳定延迟
    static millis_t serial_send_time;
    static millis_t serial_delay; 

    static uint8_t get_tsm12_id(uint8_t packet);

    // 获取Serial中发送的所有字符串并清空Serial缓存
    inline static String get_serial_string()
    {
        if (serial_ok)
            return serial_read();
        else
            return (String) "NULL";
    }

    // 获取目标字符串中某个标识后的字符串 直到空格 \n
    inline static String get_array_strings(String array, String chr)
    {
        static const char null[2] = {0};
        const String _rx_buffer = array;
        const String _chr = chr;
        const uint16_t size = _strlen_enter(_rx_buffer);
        const uint8_t chr_size = _strlen_enter(_chr);
        uint8_t error = 0;

        for (uint16_t c = 0; c < size; c++)
        {
            if (_rx_buffer[c] == _chr[0])
            {
                for (uint16_t a = 0; a < chr_size; a++)
                {
                    if (!(_rx_buffer[c + a] == _chr[a]))
                        error = 1;
                }
                if (!error)
                {
                    uint16_t array_int = size - (c + chr_size);
                    char callback[array_int + 1] = {'\n'};
                    char get;
                    for (uint16_t g = 0; g < array_int; g++)
                    {
                        get = _rx_buffer[c + chr_size + g];
                        if (get != '\n' && get != ' ' && get != 0)
                            callback[g] = get;
                    }
                    return (String)callback;
                }
            }
            error = 0;
        }
        return null;
    }

    // 获取目标字符串中是否有目标字符串 支持选择从哪里开始检查
    inline static uint8_t has_array_strings(String array, String chr, uint8_t axis = 0)
    {
        const String _rx_buffer = array;
        const String _chr = chr;
        const uint16_t size = _strlen_enter(_rx_buffer);
        const uint8_t chr_size = _strlen_enter(_chr);
        uint8_t get = 1;

        for (uint16_t c = axis; c < size; c++)
        {
            if (_rx_buffer[c] == _chr[0])
            {
                for (uint16_t a = 0; a < chr_size; a++)
                {
                    if (!(_rx_buffer[c + a] == _chr[a]))
                        get = 0;
                }
                return get;
            }
        }
        return 0;
    }

private:
    static uint8_t state;

    // 读取完整Serial信息并清空缓存
    inline static String serial_read()
    {
        String buffer;
        buffer = Serial0.readString();
        serial_clean();
        return buffer;
    }

    // Serial缓存队列长度
    inline static int serial_ok()
    {
        return Serial0.available();
    }

    // 清空Serial缓存
    inline static void serial_clean()
    {
        Serial0.flush();
    }

    // 获取字符串长度 直到空格 换行
    inline static uint16_t _strlen(const String get)
    {
        uint16_t call = 0;
        while (get[call] != '\n' && get[call] != ' ' && get[call] != 0)
            call++;
        return call;
    }

    // 获取字符串长度 直到换行
    inline static uint16_t _strlen_enter(const String get)
    {
        uint16_t call = 0;
        while (get[call] != '\n' && get[call] != 0)
            call++;
        return call;
    }
};
extern Serial_uart uart;